<template>
  <div class="app-container">
    <el-button type="primary" @click="addArticle">添加</el-button>
    <el-table
      v-loading="listLoading"
      :data="articlePageData.records"
      border
      style="width: 100%"
    >
      <el-table-column fixed="left" label="操作" width="150">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="handleUpdateShow(scope.row)"
          >编辑</el-button>
          <el-button
            type="text"
            size="small"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="articleTitle" label="标题" width="200" />
      <el-table-column prop="articleType" label="类型" width="200" />
      <el-table-column prop="createBy" label="创建人" width="100" />
      <el-table-column prop="createTime" label="创建时间" width="160" />
      <el-table-column prop="updateBy" label="更新人" width="100" />
      <el-table-column prop="updateTime" label="更新时间" width="160" />
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next, sizes, total, jumper"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="articlePageData.size"
      :total="articlePageData.total"
      @current-change="fetchData"
    />
    <el-dialog title="编辑" :visible.sync="dialogFormVisible">
      <el-form ref="articleForm" :model="formData" label-width="80px">
        <el-form-item label="文章标题">
          <el-input v-model="formData.articleTitle" />
        </el-form-item>
        <el-form-item label="类型">
          <el-select v-model="formData.articleType" placeholder="请选择">
            <el-option
              v-for="item in typeList"
              :key="item.typeId"
              :label="item.typeName"
              :value="item.typeId"
            />
          </el-select>
        </el-form-item>
        <div>
          <tinymce
            ref="articleCentontEdit"
            v-model="formData.articleContent"
            :height="300"
          />
        </div>
        <el-form-item>
          <el-button type="primary" @click="saveFormData">保存</el-button>
          <el-button @click="dialogFormVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getList, saveArticle, deleteArticle } from '@/api/blog/article'
import { getAllTypeList } from '@/api/blog/type'
import { getTargetKeyValueFromSource } from '@/utils/easyUtils'
import Tinymce from '@/components/Tinymce'

export default {
  components: { Tinymce },
  data() {
    return {
      dialogFormVisible: false,
      listLoading: true,
      articlePageData: {
        records: [],
        size: 10,
        total: 0,
        current: 1
      },
      typeList: [],
      formData: {
        articleId: 0,
        articleTitle: '',
        articleType: '',
        articleContent: ''
      }
    }
  },
  created() {
    this.fetchData(1)
    this.fetchTypeList()
  },
  methods: {
    fetchTypeList() {
      getAllTypeList().then(response => {
        this.typeList = response.data
      })
    },
    handleSizeChange(val) {
      this.articlePageData.size = val
      this.fetchData(1)
    },
    fetchData(current) {
      this.articlePageData.current = current
      this.articlePageData.records = []
      this.listLoading = true
      getList({}, this.articlePageData)
        .then(response => {
          this.articlePageData = getTargetKeyValueFromSource(
            this.articlePageData,
            response.data
          )
          this.listLoading = false
        })
        .catch(err => {
          this.listLoading = false
          console.log(err)
        })
    },
    handleUpdateShow(row) {
      this.formData = getTargetKeyValueFromSource(this.formData, row)
      if (this.$refs.articleCentontEdit) {
        this.$refs.articleCentontEdit.setContent(row.articleContent)
      }
      this.dialogFormVisible = true
    },
    handleDelete(row) {
      this.$confirm('确定删除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteArticle({ articleId: row.articleId }).then(response => {
            this.fetchData(1)
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    saveFormData() {
      this.formData.articleAbstract = this.$refs.articleCentontEdit.getText()
      if (
        this.formData.articleAbstract &&
        this.formData.articleAbstract.length > 100
      ) {
        this.formData.articleAbstract = this.formData.articleAbstract.substr(
          0,
          100
        )
      }
      saveArticle(this.formData).then(response => {
        this.dialogFormVisible = false
        this.fetchData(1)
      })
    },
    addArticle() {
      this.formData = getTargetKeyValueFromSource(this.formData, {
        articleId: 0,
        articleTitle: '',
        articleType: '',
        articleContent: ''
      })
      if (this.$refs.articleCentontEdit) {
        this.$refs.articleCentontEdit.setContent('')
      }
      this.dialogFormVisible = true
    }
  }
}
</script>
